package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IShopDAO;
import com.twentyOne.ClothingLand.vo.Shop;

public class ShopDAO extends BaseDAO implements IShopDAO {

	@Override
	public void addShop(Shop shop) {

		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(shop);
		tx.commit();
		session.close();

	}

	@Override
	public void deleteShop(Shop shop) {

		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete shop where shopId = " + shop.getShopId();
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();

	}

	@Override
	public void modifyShop(Shop shop) {

		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Shop set name=:name, phone=:phone, address=:address where shopId=:shopId";
		Query query = session.createQuery(hql);
		query.setString("name", shop.getName());
		query.setString("phone", shop.getPhone());
		query.setString("address", shop.getAddress());
		query.setInteger("shopId", shop.getShopId());
		query.executeUpdate();
		tx.commit();
		session.close();

	}

	@Override
	public List<Shop> searchAllShop() {

		Session session = getSession();
		String hql = "from Shop";
		Query query = session.createQuery(hql);
		List<Shop> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;

	}

	@Override
	public Shop searchShopByID(int shopId) {

		Session session = getSession();
		String hql = "from Shop as s where s.shopId = " + shopId;
		Query query = session.createQuery(hql);
		List<Shop> list = query.list();
		session.close();
		if(list.size() != 0) {
			Shop shop = list.get(0);
			return shop;
		}else {
			return null;
		}
			
	}

}
